package eerr.llrr.oorr.pte9983.uggtip9324

import android.app.KeyguardManager
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.os.PowerManager
import eerr.llrr.oorr.pte9983.wifi4392.jump98001.RouteJump
import yyee.llee.mmee.pl190.host668.StrongContext
import com.manager.core.pl190.host668.TurenUtils

object AdHandlerUtils {

    const val TAG = eerr.llrr.oorr.activitys.Constant3289.TAG

    val START_TAG = 1
    val TURN_TAG = 2
    val NO_SHOW = 0
    val SHOW_ONE = 1
    var indexCur = 0


    fun getCurAdInfo(): String {
        if (yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount != null && yyee.llee.mmee.pl190.host668.GlobalConfig.timerMinute != null) {
            if (indexCur < yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount.size &&
                indexCur < yyee.llee.mmee.pl190.host668.GlobalConfig.timerMinute.size
            ) {
                return "_minute:" + yyee.llee.mmee.pl190.host668.GlobalConfig.timerMinute[indexCur] +
                        "_index:" + yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount[indexCur]
            }
        }
        return ""
    }


    fun updateShowStatus(sence: String, index: Int) {
        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "updateShowStatus: sence:$sence, index:$index")
        TurenUtils.getInstance().updateAdShow()

        if (sence.contains("one") && index == 0) {
            if (yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount != null &&
                indexCur < yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount.size &&
                yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount[indexCur] > 0
            ) {
                var count = yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount[indexCur];
                count--;
                yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount[indexCur] = count;
                eerr.llrr.oorr.pte9983.b.pl190.host668.ConfigUtils.updateTimerCount()
            }
        }
    }


    val handler = Handler(Looper.getMainLooper(), object : Handler.Callback {
        override fun handleMessage(msg: Message): Boolean {
            eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "handleMessage: msg.what:${msg.what}")
            if (msg.what == START_TAG) {
                val lastAdShowTime = TurenUtils.getInstance().lastAdShow
                if (lastAdShowTime == 0L) {
                    TurenUtils.getInstance().updateAdShow()
                    turn(60 * 1000)
                    return false
                }
            }

            when (isNeedShow()) {
                NO_SHOW -> {
                }

                SHOW_ONE -> {
                    jump(yyee.llee.mmee.pl190.host668.RouteKey.TURN_TIME_ONE)
                }
            }
            turn(30000)
            return false
        }
    })


    private fun jump(turnTime: String) {
        eerr.llrr.oorr.activitys.utim986.BLog.d(
            TAG,
            "jump: turnTime:$turnTime, screenOn:${isScreenON()}"
        )
        if (isScreenON()) {
            RouteJump.safeJump(turnTime)
            return
        }

    }

    fun isScreenON(): Boolean {
        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "isScreenON: start")
        try {
            val powerManager =
                StrongContext.getApplication()
                    .getSystemService(Context.POWER_SERVICE) as PowerManager
            //true为打开，false为关闭
            val ifOpen = powerManager.isInteractive
            val mKeyguardManager =
                StrongContext.getApplication()
                    .getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
            val flag = mKeyguardManager.inKeyguardRestrictedInputMode()
            if (!(ifOpen && !flag)) {
                eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "isScreenON: false")

                return false
            }
        } catch (e: java.lang.Exception) {
        }
        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "isScreenON: true")
        return true
    }

    fun start() {
        // lanch
        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "start: ")
        handler.sendEmptyMessageDelayed(START_TAG, 10000)
    }


    fun turn(time: Long) {
        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "turn: time:$time")
        handler.sendEmptyMessageDelayed(TURN_TAG, time)
    }

    /**
     * 距离上一次是否超过 最短时间
     */
    fun isNeedShow(): Int {
        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "isNeedShow: ")
        if (yyee.llee.mmee.pl190.host668.GlobalConfig.timerMinute != null && yyee.llee.mmee.pl190.host668.GlobalConfig.timerMinute.size > 0) {
            val timeGap = System.currentTimeMillis() - TurenUtils.getInstance().lastAdShow
            for ((index, minute) in yyee.llee.mmee.pl190.host668.GlobalConfig.timerMinute.withIndex()) {
                if (timeGap >= minute * 60 * 1000 && yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount != null) {
                    if (index < yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount.size && yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount[index] > 0) {
                        indexCur = index
                        val nCount = yyee.llee.mmee.pl190.host668.GlobalConfig.timerCount[index]
                        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "isNeedShow: SHOW_ONE")

                        return SHOW_ONE
                    }
                }
            }
            if (eerr.llrr.oorr.pte9983.b.pl190.host668.ConfigUtils.initTimerCount(true)) {
                indexCur = 0;
            }
        }

        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "isNeedShow: NO_SHOW")

        return NO_SHOW
    }


    // ---------\

    // 不使用lanmda, 否则作为插件,被宿主反射调用崩溃
    val runnable = object : Runnable {
        override fun run() {
            eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "runnable: start")
            jump(yyee.llee.mmee.pl190.host668.RouteKey.UNLOCK_DELAY)
        }
    }

    fun startUnlock() {
        eerr.llrr.oorr.activitys.utim986.BLog.d(TAG, "startUnlock: ")
        handler.removeCallbacks(runnable)
        handler.postDelayed(runnable, 60 * 1000)
    }

}